import { isArray } from "@/utils/validate";

function checkArray(rule, value, callback) {
  if (!isArray(value)) {
    callback(new Error("Please select a role"));
  } else {
    callback();
  }
}

export const rules = {
  email: [{ required: true, message: "Please enter your login name", trigger: "blur" }],
  password: [
    { required: true, message: "Please input a password", trigger: "blur" },
    {
      min: 6,
      max: 20,
      message: "Length ranging from 6 to 20 characters",
      trigger: "blur"
    }
  ],
  password_confirmation: [
    { required: true, message: "Please input a password", trigger: "blur" },
    {
      min: 6,
      max: 20,
      message: "Length ranging from 6 to 20 characters",
      trigger: "blur"
    }
  ],
  roles: [{ required: true, validator: checkArray }]
};

export function Model(
  nickname = "",
  email = "",
  password = "",
  password_confirmation = "",
  roles = [],
  avatar = "",
  status = 1
) {
  this.nickname = nickname;
  this.email = email;
  this.password = password;
  this.password_confirmation = password_confirmation;
  this.roles = roles;
  this.avatar = avatar;
  this.status = status;
}

export function SearchModel() {}
